laravel 8 captcha login

Addcaptcha

To implement a Laravel 8 captcha login, you'll need to use a captcha service to generate and validate captchas. In this example, we'll use Google reCAPTCHA, one of the most popular captcha services. Here's a step-by-step guide on how to set up a captcha login in Laravel 8:


Step 1: Install Laravel 8
If you haven't already set up a Laravel 8 project, start by creating a new project using Composer:


```bash

composer create-project laravel/laravel captcha-login

cd captcha-login

```


Step 2: Set up Google reCAPTCHA
Go to the Google reCAPTCHA website (https://www.google.com/recaptcha) and sign in with your Google account. Register a new site to get the necessary keys.


Step 3: Install the necessary packages

We need to install the `guzzlehttp/guzzle` package to handle HTTP requests and `anhskohbo/no-captcha` package to integrate Google reCAPTCHA with Laravel.


```bash

composer require guzzlehttp/guzzle

composer require anhskohbo/no-captcha

```


Step 4: Configuration
In your `.env` file, add the following variables with your Google reCAPTCHA keys:


```dotenv

RECAPTCHA_SITE_KEY=your_site_key_here

RECAPTCHA_SECRET_KEY=your_secret_key_here

```


Step 5: Create Middleware

Let's create a middleware that will handle the captcha verification before allowing users to log in.


```bash

php artisan make:middleware VerifyCaptcha

```


Open the generated `VerifyCaptcha.php` middleware file in `app/Http/Middleware` and modify it as follows:


```php


namespace App\Http\Middleware;


use Closure;

use Illuminate\Http\Request;

use NoCaptcha;


class VerifyCaptcha

{

public function handle(Request $request, Closure $next)

{

$response = $request->input('g-recaptcha-response');


if (!empty($response)) {

$captcha = NoCaptcha::verifyResponse($response);


if (!$captcha->isSuccess()) {

return redirect()->back()->withErrors(['captcha' => 'Failed to verify captcha. Please try again.']);

}

} else {

return redirect()->back()->withErrors(['captcha' => 'Please complete the captcha to proceed.']);

}


return $next($request);

}

}

```


Step 6: Update Kernel.php
Next, open the `app/Http/Kernel.php` file and add the middleware to the `$routeMiddleware` array:


```php

protected $routeMiddleware = [

// other middleware

'captcha' => \App\Http\Middleware\VerifyCaptcha::class,

];

```


Step 7: Modify the Login Controller
Open the `LoginController.php` file located at `app/Http/Controllers/Auth/LoginController.php`. Update the `__construct` method to include the `captcha` middleware:


```php

public function __construct()

{

$this->middleware('guest')->except('logout');

$this->middleware('captcha')->only('login');

}

```


Step 8: Update Login Blade View

In your login view, include the Google reCAPTCHA widget.


```blade


@csrf


{{-- other login fields --}}



{!! NoCaptcha::display() !!}





```


Step 9: Update Login Route
Finally, make sure your login route in `web.php` includes the `captcha` middleware:


```php

Route::post('login', 'App\Http\Controllers\Auth\LoginController@login')->middleware('captcha');

```


That's it! Now, your Laravel 8 application should have a captcha login feature using Google reCAPTCHA. Users will need to complete the captcha verification before logging in, adding an additional layer of security to your login process.